package com.penghaonan.appframework.utils

import android.widget.Toast
import com.penghaonan.appframework.AppDelegate

object ToastUtils {
    private var ENABLE = true
    private var currentToast: Toast? = null

    fun setEnable(enable: Boolean) {
        ENABLE = enable
    }

    @JvmStatic
    fun showToast(msg: String?) {
        if (!ENABLE) {
            return
        }
        AppDelegate.post {
            showToast(
                Toast.makeText(
                    AppDelegate.getApp(),
                    msg,
                    Toast.LENGTH_SHORT
                )
            )
        }
    }

    @JvmStatic
    fun showToast(msg: Int) {
        if (!ENABLE) {
            return
        }
        AppDelegate.post {
            showToast(
                Toast.makeText(
                    AppDelegate.getApp(),
                    msg,
                    Toast.LENGTH_SHORT
                )
            )
        }
    }

    private fun showToast(toast: Toast) {
        currentToast?.cancel()
        currentToast = toast
        toast.show()
    }
}
